/**
 * 
 */
package map.model;

import java.util.ArrayList;
import java.util.List;

import map.NodeSlot;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @author balage
 *
 */
public class MapWay extends MapItem {
	
	public final List<Long> nodes = new ArrayList<Long>();
	
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof MapWay){
			return ((MapWay) obj).id == this.id;
		}
		return super.equals(obj);
	}
	
	public long getType(){
		for(String key : tags.keySet()){
			if ("boundary".equals(key))
				return NodeSlot.WTYPE_BOUNDARY;
			if ("highway".equals(key))
				return NodeSlot.WTYPE_HIGHWAY;
		}
		return NodeSlot.WTYPE_UNKNOWN;
	}
	
	public long getLevel(){
		switch((int)getType()){
		case NodeSlot.WTYPE_BOUNDARY:
			for(String key : tags.keySet()){
				if ("admin_level".equals(key)){
					return Integer.parseInt(tags.get(key));
				}
			}
		break;
		case NodeSlot.WTYPE_HIGHWAY:
		break;
		}
		return 0;
	}
	
	public long getWayType(){
		return getType()*(NodeSlot.WAYTYPE_SEPARATOR) + getLevel();
	}
	
	/**
	 * @param node
	 */
	public MapWay(Element node) {
		super(node);
		NodeList nl = node.getElementsByTagName("nd");
		for(int i = 0;i<nl.getLength();i++){
			Node n = nl.item(i);
			if (n instanceof Element){
				Element e = (Element)n;
				if ("nd".equals(e.getNodeName())){
					String ref = (e).getAttribute("ref");
					nodes.add(Long.parseLong(ref));
				}
			}
		}
	}

	@Override
	public String toString() {
		String r = "Way("+id+") ";
		for(String k : tags.keySet()){
			r += k+"="+tags.get(k)+" , ";
		}
		return r;
	}
	
}
